﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace JobTillKm6_Test_WinForms
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void executeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            JobTillKm6.Features.JobTillKm6.JobTillKm6Process proc =
                new JobTillKm6.Features.JobTillKm6.JobTillKm6Process();
            proc.Execute(Guid.NewGuid());
        }

        private void setEventReceiverToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string asmName_v1_0_0_0 = "JobTillKm6EventReceiver, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d5033a59bf4b079d";
            string asmName_v1_0_0_1 = "JobTillKm6EventReceiver, Version=1.0.0.1, Culture=neutral, PublicKeyToken=d5033a59bf4b079d";
            string className = "JobTillKm6EventReceiver.EventReceiver1.JobTillKm6EventReceiverHandler";
            SPList list = null;

            using (SPSite site = new SPSite(@"http://localhost/shops/"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    try
                    {
                        // list = web.Lists["Кассовые документы"];
                    }
                    catch
                    {
                        ;
                    }

                    if (list != null)
                    {
                        try
                        {
                            for (int i = list.EventReceivers.Count; i > 0; i--)
                            {
                                if (list.EventReceivers[i - 1].Assembly == asmName_v1_0_0_0
                                    && list.EventReceivers[i-1].Class == className
                                    )
                                {
                                    list.EventReceivers[i-1].Delete();
                                }
                                else if (list.EventReceivers[i - 1].Assembly == asmName_v1_0_0_1
                                    && list.EventReceivers[i - 1].Class == className
                                    )
                                {
                                    list.EventReceivers[i - 1].Delete();
                                }
                            }
                        }
                        catch
                        {
                            ;
                        }

                        try
                        {
                            //list.EventReceivers.Add(
                            //    SPEventReceiverType.ItemAdded, asmName_v1_0_0_0, className);
                        }
                        catch
                        {
                            ;
                        }
                    }
                }
            }
        }

        private void makeMK6ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            JobTillKm6.Features.JobTillKm6.GeneratedClass_KM6_2 o =
                new JobTillKm6.Features.JobTillKm6.GeneratedClass_KM6_2();
            o.myData.OrgAddr_Line1 = "Line 1";
            o.myData.OrgAddr_Line2 = "Line 2";
            o.CreatePackage(@"c:\Tmp\Output.xlsx");
        }

        private void testToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Exception ret = null;
            DateTime retDT = new DateTime();
            Ko2.Portal.Env env = new Ko2.Portal.Env();
            //string str_1 = "OrgAddr_Line1";
            //string str_2 = "OrgAddr_Line2";
            //string str_OrgDeparment = "OKPO";
            //string str_INN = "INN";
            //string str_OKDP = "OKDP";
            //ret = env.SetWebSetting_DocAttr_OrgAddr_Line1(str_1);
            //ret = env.GetWebSetting_DocAttr_OrgAddr_Line1(ref str_1);
            //ret = env.SetWebSetting_DocAttr_OrgAddr_Line2(str_2);
            //ret = env.GetWebSetting_DocAttr_OrgAddr_Line2(ref str_2);
            //ret = env.SetWebSetting_DocAttr_OKPO(str_OrgDeparment);
            //ret = env.GetWebSetting_DocAttr_OKPO(ref str_OrgDeparment);
            //ret = env.SetWebSetting_DocAttr_INN(str_INN);
            //ret = env.GetWebSetting_DocAttr_INN(ref str_INN);
            //ret = env.SetWebSetting_DocAttr_OKDP(str_OKDP);
            //ret = env.GetWebSetting_DocAttr_OKDP(ref str_OKDP);
            ret = env.GetWebSetting_PossDocs_KM6_LastSuccessfully(ref retDT);
        }

        private void setRightToolStripMenuItem_Click(object sender, EventArgs e)
        {

            using (SPSite site = new SPSite(@"http://localhost/sites/shop/"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList list = web.Lists["Кассовые документы"];
                    SPListItem item = list.Items[0];
                    Ko2.Portal.Right.TillDocRight.TillDocs_SetRoleItem(item);
                }
            }

        }

        private void setRightWhileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (SPSite site = new SPSite(@"http://localhost/sites/shop/"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList list = web.Lists["Кассовые документы"];
                    foreach(SPListItem item in list.Items)
                        Ko2.Portal.Right.TillDocRight.TillDocs_SetRoleItem(item);
                }
            }
        }

        private void jobInstToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SPWebApplication webApp = SPWebApplication.Lookup(new Uri(@"http://localhost/sites/shop"));
            SPServer srv = new SPServer("S-WSS02");

            //JobCreateInDocs myTJ = new JobCreateInDocs(@"JobCreateInDocs_v3_4", webApp, srv, SPJobLockType.None);
            JobTillKm6.Features.JobTillKm6.JobTillKm6Process myTJ =
                new JobTillKm6.Features.JobTillKm6.JobTillKm6Process(@"JobTillKm6_v1_7", webApp);

            SPMinuteSchedule schedule = new SPMinuteSchedule();
            schedule.BeginSecond = 0;
            schedule.EndSecond = 59;
            schedule.Interval = 1;
            //SPSchedule schedule = SPSchedule.FromString("every 20 minutes between 7:00:00 and 23:00:00");
            myTJ.Schedule = schedule;
            myTJ.Update();
        }

        private void jobDelToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SPWebApplication webApp = SPWebApplication.Lookup(new Uri(@"http://localhost/sites/shop"));
            SPServer srv = new SPServer("S-WSS02");
            foreach (SPJobDefinition job in webApp.Sites.WebApplication.JobDefinitions)
            {
                if (
                    job.Name == "JobTillKm6_v1_5"
                    || job.Name == "JobTillKm6_v1_6"
                    || job.Name == "JobTillKm6_v1_7"
                    || job.Name == "JobTillKm6_v1_8"
                    || job.Name == "JobTillKm6_v1_9"
                    || job.Name == "JobTillKm6_v1_10"
                    || job.Name == "JobTillKm6_v1_11"
                    || job.Name == "JobTillKm6_v1_12"
                    )
                {
                    job.Delete();
                }
            }
        }

        private void deleteAllItemsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (SPSite site = new SPSite(@"http://localhost/sites/shop/"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList list = web.Lists["Кассовые документы"];
                    for (int i = list.Items.Count - 1; i >= 0; i--)
                    {
                        list.Items[i].Delete();
                    }
                }
            }
        }

    }
}
